package com.redoor.iot.controller;


import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redoor.iot.aspect.Log;
import com.redoor.iot.model.Menu;
import com.redoor.iot.model.Result;
import com.redoor.iot.model.Tree;
import com.redoor.iot.service.IMenuService;
import com.redoor.iot.utils.ResultUtil;
import com.redoor.iot.utils.SessionUtil;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value="/login")
public class LoginController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	IMenuService menuService;

	@Autowired
	private ObjectMapper objectMapper;
	
	@Log("左侧菜单树")
	@GetMapping("/menuindex")
	@ResponseBody
	Result index(HttpServletRequest request, HttpServletResponse response) throws JsonParseException, JsonMappingException, IOException  {
		
		List<Tree<Menu>> menus = menuService.listMenuTree(SessionUtil.getUser().getUserId());
		logger.info("登录用户名{}",SessionUtil.getUser().getUserName());
		//response.setContentType("application/json;charset=UTF-8");
		//response.getWriter().write(objectMapper.writeValueAsString(menus));
		return ResultUtil.success(menus);
	}

	@GetMapping("/login")
	String login() {
		return "login";
	}
	
	

	/*@PostMapping("/login")
	@ResponseBody
	Result ajaxLogin(String username, String password) {
		password = MD5Utils.encrypt(password);
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			return ResultUtil.success();
		} catch (AuthenticationException e) {
			return ResultUtil.error(5, "用户或密码错误");
		}
	}*/
	
	@GetMapping("/logout")
	String logout() {
		/*ShiroUtils.logout();*/
		return "redirect:/login";
	}

	
}
